app.controller('collection', function ($scope, $http) {

    var token = localStorage.getItem('token');
    $scope.collectionInfos = [];

    //查看我的收藏
    $scope.myCollection = function () {
        var infiniteScrollPreloader = $('.infinite-scroll-preloader');

        if (!token) {
            $.toast("请先登录");
            location.href = '#user-login';
            return;
        }

        if (token) {
            $http({
                method: 'get',
                url: app.netPath + '/sc/show',
                headers: {Authorization: token},
                params: {scType: 0}
            }).then(function (res) {
                // 删除加载提示符
                infiniteScrollPreloader.hide();
                if (!res.data.error) {
                    console.log(res.data.data);
                    $scope.collectionInfos = res.data.data;
                    for (var i = 0; i < $scope.collectionInfos.length; i++) {
                        $scope.collectionInfos[i].aClass.classImg = app.netPath + '/resources/classImg/' + $scope.collectionInfos[i].aClass.classImg;
                        app.buildImageUrl($scope.collectionInfos[i].aClass,'classImg',function () {
                            $scope.$digest();
                        });
                    }
                }else  if(res.data.error==105){
                    $scope.listSize=0;
                }
                else {
                    $.toast(res.data.msg);
                }
            },function(){
                $.toast('网络走丢了！')
            })
        }

    };


    //得到scId，删除收藏
    $scope.getScId = function (scId) {
        $.confirm('确认删除此收藏？', '', function () {

            $http({
                method: 'get',
                url: app.netPath + '/sc/delete',
                headers: {Authorization: token},
                params: {scIds: scId, scType: 0}
            }).then(function (res) {
                if (!res.data.error) {
                    location.href = '#collection';
                    $.toast(res.data.msg)
                }
                else {
                    $.toast(res.data.msg);
                }
            })
        }, function () {
            return;
        })

    };

    //得到classId
    $scope.getClassId = function (classId) {
        sessionStorage.setItem('classId', classId);
        location.href = '#xiangqing';

    };


});
